using System;
using Microsoft.Xna.Framework;
using Rocuna.HID.Interfaces;

namespace Rocuna.HID.Core
{
    /// <summary>
    /// Base class for Input device.
    /// </summary>
    public abstract class BaseInputDevice : GameComponent, IInputDevice
    {
        protected bool _isActive = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseInputDevice"/> class.
        /// </summary>
        /// <param name="game">Game that the game component should be attached to.</param>
        protected BaseInputDevice(Game game)
            : base(game)
        {
        }

        #region Implementation of IInputDevice

        /// <summary>
        /// Gets the name of the device.
        /// </summary>
        /// <value>
        /// The name of the device.
        /// </value>
        public abstract string DeviceName { get; }

        /// <summary>
        /// Polls this device for input status.
        /// </summary>
        public virtual void Poll()
        {
        }

        /// <summary>
        /// Registers action to specified event.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="action">The action.</param>
        public virtual void RegisterAction(object eventType, Action<object> action)
        {
        }

        /// <summary>
        /// UnRegister action to specified event.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        public virtual void UnRegisterAction(object eventType)
        {
        }

        /// <summary>
        /// Gets a value indicating whether this instance is active.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is active; otherwise, <c>false</c>.
        /// </value>
        public virtual bool IsActive
        {
            get { return _isActive; }
        }

        /// <summary>
        /// Unactive the device.
        /// </summary>
        public void DeActiveDevice()
        {
            _isActive = false;
            Game.Components.Remove(this);
        }

        /// <summary>
        /// Activates the device.
        /// </summary>
        public void ActivateDevice()
        {
            _isActive = true;
            Game.Components.Add(this);
        }

        /// <summary>
        /// Clears this instance actions.
        /// </summary>
        public virtual void Clear()
        {

        }

        #endregion

        public override void Update(GameTime gameTime)
        {
            Poll();
            base.Update(gameTime);
        }
    }
}